package com.lsyw.intergrate.controller;

import cn.dev33.satoken.stp.StpUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.lsyw.intergrate.common.redis.RedisUtil;
import com.lsyw.intergrate.entity.JcMassifFactor;
import com.lsyw.intergrate.entity.T0200;
import com.lsyw.intergrate.service.T0200Service;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.util.StringUtils;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.lsyw.intergrate.entity.common.PageInfo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lsyw.intergrate.common.Result;
import org.springframework.util.CollectionUtils;
import lombok.AllArgsConstructor;
import lombok.NoArgsConstructor;
import com.lsyw.intergrate.service.TransportWorkOrderService;
import com.lsyw.intergrate.entity.TransportWorkOrder;

import java.util.*;

import io.swagger.annotations.Api;
import lombok.Data;

import org.springframework.stereotype.Controller;

import javax.annotation.Resource;

/**
 * <p>
 * 运输工单信息 前端控制器
 * </p>
 *
 * @author 刘磊
 * @since 2023-11-29
 */
@Api(tags = "运输工单信息")
@RestController
@RequestMapping("/transport-work-order")
public class TransportWorkOrderController {


    @Autowired
    private TransportWorkOrderService transportWorkOrderService;

    @Autowired
    private T0200Service t0200Service;

    @Resource
    private RedisUtil redisUtil;

    /**
     * 分页查询
     */
    @PostMapping("/workOrderPage")
    @ApiOperation(value = "运输历史记录列表-分页", notes = "运输历史记录列表-分页", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "condition:companyId", value = "运输公司/团队 ", required = false, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "condition:massifId", value = "地块Id", required = false, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "condition:name", value = "工单名称", required = false, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "condition:startTime", value = "开始时间  年-月-日", required = false, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "condition:endTime", value = "结束时间 年-月-日", required = false, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "condition:transportPlanId", value = "运输计划", required = false, paramType = "query", dataType = "String")
    })
    public Result workOrderPage(@RequestBody PageInfo pageInfo) {
        Page<TransportWorkOrder> page = new Page<>();
        page.setSize(pageInfo.getPageSize());
        page.setCurrent(pageInfo.getPageNum());
        return Result.successResult(this.transportWorkOrderService.pageAppList(page, pageInfo.getCondition()));
    }
    /**
     * 分页查询
     */
    @GetMapping("/getWorkOrderDetail")
    @ApiOperation(value = "运输历史明细", notes = "运输历史明细", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "workOrderId", value = "工单Id", required = false, paramType = "query", dataType = "String")
    })
    public Result getWorkOrderDetail(String workOrderId) {
        return Result.successResult(this.transportWorkOrderService.getWorkOrderDetail(workOrderId));
    }

    /**
     * 通过id查询最新gps
     */
    @GetMapping("/get-gps-by-order-id")
    @ApiOperation(value = "通过id查询最新gps", notes = "通过id查询最新gps", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "workOrderId", value = "工单Id", required = false, paramType = "query", dataType = "String")
    })
    public Result getById(String workOrderId) {
        TransportWorkOrder order = this.transportWorkOrderService.getById(workOrderId);
        String gpsSoilId = order.getGpsSoilId();
        String gpsVehicleId = order.getGpsVehicleId();

        String soilGPS = redisUtil.get(gpsSoilId+workOrderId);
        String vehicleGPS = redisUtil.get(gpsVehicleId+workOrderId);
        //如果没值
        if (StringUtils.hasLength(soilGPS)){
            LambdaQueryWrapper<T0200> lambda = new QueryWrapper<T0200>().lambda();
            lambda.eq(T0200::getNo ,gpsSoilId);
            lambda.eq(T0200::getOrderId ,workOrderId);
            lambda.orderByDesc(T0200::getGmtCreate);
            List<T0200> list = this.t0200Service.list(lambda);
            if (list!=null  && list.size()>0){
                soilGPS= JSON.toJSONString(list.get(0));
            }
        }
        if (StringUtils.hasLength(vehicleGPS)){
            LambdaQueryWrapper<T0200> lambda = new QueryWrapper<T0200>().lambda();
            lambda.eq(T0200::getNo ,gpsVehicleId);
            lambda.eq(T0200::getOrderId ,workOrderId);
            lambda.orderByDesc(T0200::getGmtCreate);
            List<T0200> list = this.t0200Service.list(lambda);
            if (list!=null  && list.size()>0){
                vehicleGPS= JSON.toJSONString(list.get(0));
            }
        }

        HashMap<String, Object > map = new HashMap<>();
        map.put("soilGPS", JSON.parseObject(soilGPS));
        map.put("vehicleGPS",JSON.parseObject(vehicleGPS));
        return Result.successResult(map);
    }

}
